package com.hzgj.bcl.stats;

import com.hzgj.bcl.redis.RedisClient;

/**
 * Created by Administrator on 2016/8/23.
 */
public class StatsHandler {

    public static final int expire = 15 * 24 * 60 * 60;//15天

    public static void handleDataWithRedis(RedisClient redisClient, String key, Metric metric, long time) {
        Snapshot snapshot = metric.getTimeCounter().getSnapshot();
        long total = metric.getMethodCounter().getTotalCount();
        long success = metric.getMethodCounter().getSuccessCount();
        redisClient.pipeline(pipeline -> {
            pipeline.zadd(RedisKey.getErrorCount(key), time, (total - success) + "_" + time);
            pipeline.zadd(RedisKey.getTotalCount(key), time, total + "_" + time);
            pipeline.zadd(RedisKey.getMaxSpan(key), time, snapshot.getMax() + "_" + time);
            pipeline.zadd(RedisKey.getMinSpan(key), time, snapshot.getMin() + "_" + time);
            pipeline.zadd(RedisKey.getMeanSpan(key), time, snapshot.getMean() + "_" + time);
            pipeline.zadd(RedisKey.getSpanTp75(key), time, snapshot.get75thPercentile() + "_" + time);
            pipeline.zadd(RedisKey.getSpanTp95(key), time, snapshot.get95thPercentile() + "_" + time);
            pipeline.zadd(RedisKey.getSpanTp99(key), time, snapshot.get99thPercentile() + "_" + time);
            pipeline.zadd(RedisKey.getSpanTp999(key), time, snapshot.get999thPercentile() + "_" + time);

            pipeline.expire(RedisKey.getErrorCount(key), expire);
            pipeline.expire(RedisKey.getTotalCount(key), expire);
            pipeline.expire(RedisKey.getMaxSpan(key), expire);
            pipeline.expire(RedisKey.getMinSpan(key), expire);
            pipeline.expire(RedisKey.getMeanSpan(key), expire);
            pipeline.expire(RedisKey.getSpanTp75(key), expire);
            pipeline.expire(RedisKey.getSpanTp95(key), expire);
            pipeline.expire(RedisKey.getSpanTp99(key), expire);
            pipeline.expire(RedisKey.getSpanTp999(key), expire);

        });
    }

    public static void handleDataWithRedis(RedisClient redisClient, String key, Metric metric) {
        handleDataWithRedis(redisClient, key, metric, metric.getTarget());
    }
}
